home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH06 / PGM6_5.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-10-10  |  3.1 KB  |  132 lines

  1. ; Bit Operations and SETcc Instructions
  2.  
  3.         .386            ;So we can use extended registers
  4.         option    segment:use16    ; and addressing modes.
  5.  
  6. dseg        segment    para public 'data'
  7.  
  8. ; Some type definitions for the variables we will declare:
  9.  
  10. uint        typedef    word        ;Unsigned integers.
  11. integer        typedef    sword        ;Signed integers.
  12.  
  13.  
  14. ; Some variables we can use:
  15.  
  16. j        integer    ?
  17. k        integer    ?
  18. u1          uint    2
  19. u2        uint    2
  20. Result        byte    ?
  21.  
  22. dseg        ends
  23.  
  24.  
  25.  
  26. cseg        segment    para public 'code'
  27.         assume    cs:cseg, ds:dseg
  28.  
  29. Main        proc
  30.         mov    ax, dseg
  31.         mov    ds, ax
  32.         mov    es, ax
  33.  
  34. ; Initialize some variables
  35.  
  36.         mov    j, -2
  37.         mov    k, 2
  38.  
  39. ; The SETcc instructions store a one or zero into their operand if the
  40. ; specified condition is true or false, respectively.  The TEST instruction
  41. ; logically ANDs its operands and sets the flags accordingly (in particular,
  42. ; TEST sets/clears the zero flag if there is/isn't a zero result).  We can
  43. ; use these two facts to copy a single bit (zero extended) to a byte operand.
  44.  
  45.         test    j, 11000b    ;Test bits 4 and 5.
  46.         setne    Result        ;Result=1 if bits 4 or 5 of J are 1.
  47.  
  48.         test    k, 10b        ;Test bit #1.
  49.         sete    Result        ;Result=1 if bit #1 = 0.
  50.  
  51. ; The SETcc instructions are particularly useful after a CMP instruction.
  52. ; You can set a boolean value according to the result of the comparison.
  53. ;
  54. ; Result := j <= k
  55.  
  56.         mov    ax, j
  57.         cmp    ax, k
  58.         setle    Result        ;Note that "le" is for signed values.
  59.  
  60. ; Result := u1 <= u2
  61.  
  62.         mov    ax, u1
  63.         cmp    ax, u2
  64.         setbe    Result        ;Note that "be" is for unsigned values.
  65.  
  66. ; One thing nice about the boolean results that the SETcc instructions
  67. ; produce is that we can AND, OR, and XOR them and get the same results
  68. ; one would expect in a HLL like C, Pascal, or BASIC.
  69. ;
  70. ; Result := (j < k) and (u1 > u2)
  71.  
  72.         mov    ax, j
  73.         cmp    ax, k
  74.         setl    bl        ;Use "l" for signed comparisons.
  75.  
  76.         mov    ax, u1
  77.         cmp    ax, u2
  78.         seta    al        ;Use "a" for unsigned comparisons.
  79.  
  80.         and    al, bl        ;Logically AND the two boolean results
  81.         mov    Result, al    ; and store the result away.
  82.  
  83. ; Sometimes you can use the shift and rotate instructions to test to see
  84. ; if a specific bit is set.  For example, SHR copies bit #0 into the carry
  85. ; flag and SHL copies the H.O. bit into the carry flag.  We can easily test
  86. ; these bits as follows:
  87. ;
  88. ; Result := bit #15 of J.
  89.  
  90.         mov    ax, j
  91.         shl    ax, 1
  92.         setc    Result
  93.  
  94. ; Result := bit #0 of u1:
  95.  
  96.         mov    ax, u1
  97.         shr    ax, 1
  98.         setc    Result
  99.  
  100. ; If you don't have an 80386 or later processor and cannot use the SETcc
  101. ; instructions, you can often simulate them.  Consider the above two
  102. ; sequences rewritten for the 8086:
  103.  
  104. ;
  105. ; Result := bit #15 of J.
  106.  
  107.         mov    ax, j
  108.         rol    ax, 1            ;Copy bit #15 to bit #0.
  109.         and    al, 1            ;Strip other bits.
  110.         mov    Result, al
  111.  
  112. ; Result := bit #0 of u1:
  113.  
  114.         mov    ax, u1
  115.         and    al, 1            ;Strip unnecessary bits.
  116.         mov    Result, al
  117.  
  118. Quit:        mov    ah, 4ch            ;DOS opcode to quit program.
  119.         int    21h            ;Call DOS.
  120. Main        endp
  121.  
  122. cseg        ends
  123.  
  124. sseg        segment    para stack 'stack'
  125. stk        byte    1024 dup ("stack   ")
  126. sseg        ends
  127.  
  128. zzzzzzseg    segment    para public 'zzzzzz'
  129. LastBytes    byte    16 dup (?)
  130. zzzzzzseg    ends
  131.         end    Main
  132.